package secuDropper.core.descriptor;

import java.io.File;
import java.io.InputStream;
import java.util.Arrays;

import secuDropper.core.security.SecurityHandler;

/**
 * The entry point of the secuDropper core part. It holds the root of the files
 * which have to be secured.
 * 
 * @author Matthias Hauser
 */
public class Descriptor {

	public static final String DESCRIPTOR_FILE_NAME = "descriptor.sd";

	private final SecurityHandler securityHandler;

	/**
	 * @param root
	 *            the root directory of the encrypted files. must contain the
	 *            descriptor file
	 * @param password
	 *            the password which is used by the user to de- and encrypt the
	 *            data
	 */
	public Descriptor(final File root, final String password) {
		if (root == null)
			throw new IllegalArgumentException("root must not be null");
		if (!root.exists() || !root.isDirectory())
			throw new IllegalArgumentException("root must be a directory");
		if (Arrays.binarySearch(root.list(), DESCRIPTOR_FILE_NAME) < 0)
			throw new IllegalArgumentException(
					"root directory must contain a descriptor file named "
							+ DESCRIPTOR_FILE_NAME);

		securityHandler = new SecurityHandler(password);

	}

	protected SecurityHandler getSecurityHandler() {
		return securityHandler;
	}

	/**
	 * Initiate the {@link Descriptor} with a reference of a {@link File} on the
	 * filesystem.
	 * 
	 * @param f
	 *            the descriptor file, named as Descriptor.DESCRIPTOR_FILE_NAME
	 * @return true if the initialization was successful, false otherwise
	 */
	public boolean init(final File f) {
		// TODO
		return true;
	}

	/**
	 * Initiate the {@link Descriptor} with an {@link InputStream}
	 * 
	 * @param inputStream
	 *            of the descriptor file.
	 * @return true if the initialization was successful, false otherwise
	 */
	public boolean init(InputStream inputStream) {
		// TODO
		return true;
	}

}
